package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.projectcore.CustomerPoolCacheKeyBuilder;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.imaster.dao.CustomerPoolMapper;
import com.eastfair.imaster.dto.CustomerPoolSaveDTO;
import com.eastfair.imaster.entity.CustomerPool;
import com.eastfair.imaster.service.CustomerPoolService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

/**
 * <p>
 * 业务实现类
 * 客户池
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-11-15
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class CustomerPoolServiceImpl extends SuperCacheServiceImpl<CustomerPoolMapper, CustomerPool> implements CustomerPoolService {


    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new CustomerPoolCacheKeyBuilder();
    }

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(CustomerPool entity) {
        UpdateWrapper<CustomerPool> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(CustomerPool::getProjectId, entity.getProjectId()).eq(CustomerPool::getId, entity.getId());
        entity.setProjectId(null);
        //删除缓存
        delCache(entity);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<CustomerPool> modelList) {
        modelList.forEach(s -> s.setIsDelete(BusinessConstant.YES));
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }


    @Override
    public Boolean addPool(CustomerPoolSaveDTO saveDTO) {

        return null;
    }
}
